package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.HotArticleService;
import com.heima.common.redis.CacheService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.HotArticleVo;
import com.heima.model.common.constants.ArticleConstants;
import com.heima.utils.common.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author rookie001
 * @creat 2023-06-24-15:18
 */
@Service
@Slf4j
@Transactional
public class HotArticleServiceImpl implements HotArticleService {
    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private CacheService cacheService;


    //热点文章查询
    @Override
    public void computeHotArticle() {
        //查询五天前的文章数据
        Date date = DateTime.now().minusDays(5).toDate();
        List<ApArticle> apArticles = apArticleMapper.queryArticleFiveday(date);
        //给文章算分
        List<HotArticleVo> hotArticleVos = apArticles.stream().map(apArticle -> {
            HotArticleVo hotArticleVo = new HotArticleVo();
            BeanUtils.copyProperties(apArticle, hotArticleVo);
            Integer score = computeScore(apArticle);
            hotArticleVo.setScore(score);
            return hotArticleVo;
        }).collect(Collectors.toList());
        //将热点文章按照 频道存入缓存
        //查询当前频道
        List<Integer> channes = apArticles.stream().map(ApArticle::getChannelId).collect(Collectors.toList());
        for (Integer channeId : channes) {
            //根据文章根据频道 分数倒叙保留30个
            List<HotArticleVo> hotArticleVoList = hotArticleVos.stream()
                    .sorted((o1, o2) -> o2.getScore() - o1.getScore())
                    .filter(apArticle -> apArticle.getChannelId().equals(channeId))
                    .limit(30).collect(Collectors.toList());
            cacheService.set(channeId+ArticleConstants.HOT_ARTICLE_FIRST_PAGE,JSON.toJSONString(hotArticleVoList));
        }
        //将热点文章按照 推荐存入缓存
        List<HotArticleVo> hotArticleVoList = hotArticleVos.stream().sorted((o1, o2) -> o2.getScore() - o1.getScore()).limit(30).collect(Collectors.toList());
        cacheService.set(ArticleConstants.DEFAULT_TAG+ArticleConstants.HOT_ARTICLE_FIRST_PAGE,JSON.toJSONString(hotArticleVoList));
    }
    //文章算分方法
    private Integer computeScore(ApArticle apArticle) {
        Integer score=0;
        if (apArticle.getLikes()>0){
            score+=apArticle.getLikes()*3;
        }
        if (apArticle.getComment()>0){
            score+=apArticle.getComment()*5;
        }
        if (apArticle.getCollection()>0){
            score+=apArticle.getCollection()*8;
        }
        if (apArticle.getViews()>0){
            score+=apArticle.getViews();
        }
        return score;
    }
}
